package fr.umlv.android.boggle;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import fr.umlv.android.boggle.model.Letter;


@SuppressWarnings("serial")
public class Word implements Serializable {
	private final StringBuilder stringB;
	private List<Letter> letters;

	public Word() {
		stringB = new StringBuilder();
		letters = new ArrayList<Letter>();
	}
	
	public void addLetter(Letter letter) {
		letters.add(letter);
		stringB.append(letter.getChar());
		System.out.println("STRING BUILDE  LENGTH = "+ stringB.length());

	}
	
	public int indexOf(Letter letter) {
		return letters.indexOf(letter);
	}
	
	@Override
	public String toString() {
		return stringB.toString();
	}
	
	public Letter getLetter(int index) {
		return letters.get(index);
	}
	
	public Letter firstLetter() {
		if(letters.size()==0) {
			return null;
		}
		return letters.get(0);
	}
	
	public Letter lastLetter() {
		if(letters.size()==0) {
			return null;
		}
		return letters.get(letters.size()-1);
	}

	public int size() {
		return letters.size();
	}
	
	public boolean contains(Letter letter) {
		return letters.contains(letter);
	}

	public Iterator<Letter> iterator() {
		return letters.iterator();
	}

	public boolean remove(Letter letter) {
		int index = letters.indexOf(letter);
		System.out.println("LETTER "+letter.getChar());
		return remove(index);
	}
	
	public boolean remove(int index) {
		System.out.println("REMOVE INDEX = "+index +", LENGTH = "+ stringB.length());
		stringB.deleteCharAt(index);
		return letters.remove(index) != null;
	}
	

	public void clear() {
		clear(0);
	}
	
	public void clear(int start) {
		letters = letters.subList(0, start);
		stringB.delete(start, stringB.length());
	}
}
